<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<div class="panel panel-default">
	<div class="panel-heading">
		<h3 class="panel-title">二维码生成</h3>
	</div>
	<div class="panel-body">
		<form class="form-horizontal" role="form" id="form">
			<input type="hidden"  id="fileName"/>
			<input type="hidden"  id="fileSavePath"/>
			<div class="form-group">
				<label for="mode" class="col-xs-12 col-sm-3 control-label no-padding-right">生成方式:</label>
				<div class="col-xs-12 col-sm-4">
					<select class="form-control" id="mode" style="width: 95%;">
						<option value="0">手工输入</option>
						<option value="1">上传文件</option>
					</select>
				</div>
			</div>
			<div class="space-4"></div>
			<div class="form-group">
				<label for="serialNumber" class="col-xs-12 col-sm-3 control-label no-padding-right">设备序列号:</label>
				<div class="col-xs-12 col-sm-4">
					<input type="text" id="serialNumber" class="width-100"/>
					<span class="required">*</span>
				</div>
			</div>
			<div class="form-group" style="display: none;">
				<label for="swfUploadContainer" class="col-xs-12 col-sm-3 control-label no-padding-right">上传文件:</label>
				<div class="col-xs-12 col-sm-4">
					<div id="swfUploadContainer" style="width: 95%;">
					
					</div>
					<div style="text-align: center; color: #b74635;">温馨提示：请上传txt格式的文件,一个序列号一行</div>
				</div>
			</div>
			<div class="space-12"></div>
			<div class="form-group" style="text-align: center;">
				<div class="col-sm-9">
					<button id="saveBtn" type="button" class="btn btn-success btn-sm no-border">
						<i class="fa fa-check"></i>生成
					</button>
				</div>
			</div>
		</form>
	</div>
</div>
<script>
	$(function(){
		$('#mode').on('change', function(){
			var $this = $(this);
			if($this.val() == 0){
				$('#serialNumber').closest('.form-group').show();
				$('#swfUploadContainer').closest('.form-group').hide();
			}else{
				$('#serialNumber').closest('.form-group').hide();
				$('#swfUploadContainer').closest('.form-group').show();
			}
		});
		pageContents.find('#swfUploadContainer').tinyUploader({
				url : '<%=request.getContextPath()%>/qrcode/upload',
				button_image_url: contextPath + '/images/uploadButton.jpg',
				dataType : 'text',
				onUploadSuccess : function(id, data) {
					var data = JSON.parse(data);
					pageContents.find('#fileName').val(data.data.fileName);
					pageContents.find('#fileSavePath').val(data.data.fileSavePath);
				}
		});
		$('#saveBtn').on('click', function(){
			var $btn = $(this);
			var mode = $('#mode').val();
			if(mode == 0){
				var serialNumber = $('#serialNumber').val().replace(/(^\s*)|(\s*$)/g, "");
				if(serialNumber.length == 0){
					$('body').message({
						type: 'warning',
						content: '请输入序列号!'
					});
					return;
				}
				if(!/^[0-9a-zA-Z-]+$/.test(serialNumber)){
					$('body').message({
						type: 'warning',
						content: '序列号不合法!'
					});
					return;
				}
				$btn.attr('disabled', 'disabled').addClass('disabled');
				$('body').loader({
					title: '正在生成二维码...',
					opacity: 0.2
				});
  				$.post('<%=request.getContextPath()%>/qrcode/get/'+serialNumber).done(function(data){
  					$('body').loader('hide');
  					$btn.removeAttr('disabled').removeClass('disabled');
  					if(data.statusCode == 20011011){
  						$.get(contextPath + '/qrcode/modal').done(function(html) {
							var dialog = $(html).modal({
								keyboard: false
							}).on({
								'hidden.bs.modal': function(){
									$(this).remove();
								}
							});
							dialog.find('#saveBtn').attr('href', '<%=request.getContextPath()%>/qrcode/download?fileName='+data.data.fileName)
								.on('click', function(){
									dialog.modal('hide');
									$('#menuList [data-title="二维码生成"]').click();
								});
						});
  					}else {
						pageContents.find('#form').message({
							type : 'error',
							content : data.msg
						});
					}
  				});
			}else{
  				var fileName = pageContents.find('#fileName').val();
  				if(fileName.length == 0){
  					$('body').message({
  						type: 'warning',
  						content: '请上传二维码文件!'
  					});
  					return;
  				}
  				$btn.attr('disabled', 'disabled').addClass('disabled');
  				var data = {
  					fileSavePath: pageContents.find('#fileSavePath').val(),
  					fileName: fileName
  				}
  				$('body').loader({
					title: '正在生成二维码...',
					opacity: 0.2
				});
  				$.post('<%=request.getContextPath()%>/qrcode/get', data).done(function(data){
  					$('body').loader('hide');
  					$btn.removeAttr('disabled').removeClass('disabled');
  					if(data.statusCode == 20011011){
  						$.get(contextPath + '/qrcode/modal').done(function(html) {
  							var dialog = $(html).modal({
  								keyboard: false
  							}).on({
  								'hidden.bs.modal': function(){
  									$(this).remove();
  								}
  							});
  							dialog.find('#saveBtn').attr('href', '<%=request.getContextPath()%>/qrcode/download?fileName='+data.data.fileName)
  								.on('click', function(){
  									dialog.modal('hide');
  									$('#menuList [data-title="二维码生成"]').click();
  								});
  						});
  					}else {
  						pageContents.find('#form').message({
  							type : 'error',
  							content : data.msg
  						});
  					}
  				});
			}
		});
	});
</script>